var yzmval = '';
function submitlogin(){
	var pass = get('pass').value;
	var user = get('user').value;
	if(!user){js.setmsg('请输入帐号');return;}
	if(!pass){js.setmsg('请输入密码');return;}
	var o1 = get('loginbtn');
	js.setmsg('登录中...');
	o1.disabled = true;
	js.setoption('adminuser', user);
	js.setoption('adminpass', pass);
	js.ajax.post({
		url:'?m=login&a=check',
		ntoken:true,
		data:{pass:pass,user:user,cfrom:CFROM,web:js.web.gettype(),device:device,yzmval:yzmval},
		dataType:'json',
		onerror:function(ret){
			js.show.hide();
			js.setmsg(ret.msg);
			if(ret.code==190){
				loginyzm(ret.data,ret.msg);
			}
			o1.disabled = false;
		}
	},function(ret){
		js.setoption('token',ret.data.token);
		js.setmsg('登录成功跳转中...');
		js.reload();
	});
	yzmval = '';
}

function loginyzm(ds,bt){
	js.modal.main({
		title:bt,action:'login',
		width:300,height:300,
		html:'<div style="padding:20px"><div>'+ds.msg+'</div><div class="flex webbtn-group"><input maxlength="6" class="input" id="valyzmval" autocomplete="off" placeholder="请输入验证码" /><button type="button" onclick="getcode(this,\''+ds.mobile+'\')" class="webbtn webbtn-default">获取</button></div></div>',
		btn:[{text:'登录'}]
	});
	$('#login_btn0').click(function(){
		var val = get('valyzmval').value;
		if(!val)return js.show.error('请输入验证码');
		yzmval = val;
		js.tanclose('login');
		submitlogin();
	});
}

function getcode(o1, sj){
	o1.innerHTML='获取中';
	o1.disabled=true;
	js.ajax.get({
		url:'?m=login&a=getcode',
		data:{mobile:sj,device:device},
		onerror:function(ret){
			o1.innerHTML='重新获取';
			o1.disabled=false;
			if(ret.data && ret.data.time){
				getcodes(o1, ret.data.time);
			}
		}
	}, function(ret){
		o1.innerHTML='获取成功';
		js.msg('success','验证码已发送');
		getcodes(o1, 60);
	});
}
function getcodes(o1, sj){
	if(!o1)return;
	if(sj<=0){
		o1.innerHTML='再次获取';
		o1.disabled=false;
	}else{
		o1.innerHTML=''+sj+'';
		o1.disabled=true;
		setTimeout(function(){
			getcodes(o1, sj-1);
		},1000);
	}
}

function initbody(){
	if(!get('user'))return;
	var val = js.getoption('adminuser');
	if(val)get('user').value = val;
	val = js.getoption('adminpass');
	if(val)get('pass').value = val;
	
	$(get('user')).keydown(function(e){
		if(e.keyCode==13)get('pass').focus();
	});
	$(get('pass')).keydown(function(e){
		if(e.keyCode==13)submitlogin();
	});
	js.setoption('token','');
	token = '';
}

function loginexit(){
	js.confirm('确定要退出吗？',function(jg){
		if(jg=='yes'){
			js.loading('退出中...');
			js.ajax.get('?m=geren&a=exitcheck', function(){
				js.show.success('退出成功');
				js.reload();
			});
		}
	});
}